document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady()
{
	 FB.init({
                  appId: '521386087932255',
                  nativeInterface: CDV.FB,
                  status     : true, // check login status
                  //cookie     : true, // enable cookies to allow the server to access the session
                 // xfbml: true,
                  useCachedDialogs: false
              });
              FB.Event.subscribe('auth.statusChange', handleStatusChange);
              FB.getLoginStatus(handleStatusChange);
			  promptLogin();
}

function promptLogin() {
  FB.login(null, {scope: 'email'});
}

function handleStatusChange(response) {
  if (response.authResponse) {
    console.log(response);
    window.location.hash = '#menu';
    updateUserInfo(response);
  } else {
    console.log(response);
    window.location.hash = '#login';
  }
}

function updateUserInfo(response) {
  FB.api('/me', 
    {fields:"name,first_name,picture"},
    function(response) {
      console.log(response);
      var output = '';
      output += '<img src="' + response.picture.data.url + '" width="25" height="25"></img>';
      output += ' ' + response.first_name;
      $('#user-identity').html(output);
  });
}